{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "api_secret = \"b6farN7EvYgB6GcXSwVXk_IKmgtC8plC\"\n",
    "api_key = 'JUAq1CxnuWYYHG0KG7X2RufUL78KuG0K'\n",
    "\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "img_url = 'https://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg'\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603519387,235e2db6-8b0a-4c2b-a27d-a492e2a18b24\",\"time_used\":1535,\"faces\":[{\"face_token\":\"58cd8647acb8e497e1b5eab9836d7fd1\",\"face_rectangle\":{\"top\":161,\"left\":109,\"width\":188,\"height\":188},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":21},\"smile\":{\"value\":0.027,\"threshold\":50.000},\"emotion\":{\"anger\":0.307,\"disgust\":0.142,\"fear\":0.038,\"happiness\":0.280,\"neutral\":98.710,\"sadness\":0.150,\"surprise\":0.373}}}],\"image_id\":\"WDzfrL9g9smt0d3OoYw31A==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>58cd8647acb8e497e1b5eab9836d7fd1</td>\n",
       "      <td>161</td>\n",
       "      <td>109</td>\n",
       "      <td>188</td>\n",
       "      <td>188</td>\n",
       "      <td>Male</td>\n",
       "      <td>21</td>\n",
       "      <td>0.027</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.307</td>\n",
       "      <td>0.142</td>\n",
       "      <td>0.038</td>\n",
       "      <td>0.28</td>\n",
       "      <td>98.71</td>\n",
       "      <td>0.15</td>\n",
       "      <td>0.373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  58cd8647acb8e497e1b5eab9836d7fd1                 161                  109   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   188                    188                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    21                   0.027                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.307                       0.142   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.038                          0.28   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       98.71                        0.15   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.373  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() \n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
